package it.rockliveitalia;

import it.rockliveitalia.database.EventDbHelper;
import it.rockliveitalia.entity.Event;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;

import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.widget.DrawerLayout;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Point;

public class MapFilterActivity extends MapActivity {

	private List<String> ids;
	protected boolean fileLoaded = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map_filter);
		context = this.getApplicationContext();
		setSupportProgressBarIndeterminateVisibility(false);
		int status = GooglePlayServicesUtil
				.isGooglePlayServicesAvailable(getBaseContext());
		if (status != ConnectionResult.SUCCESS) {
			// Google Play Services are
			// not available
			googlePlayServices = false;

		} else {
			// Google Play Services are available
			SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.map_filter);
			googleMap = fm.getMap();

			Intent intent = getIntent();
			ids = intent.getStringArrayListExtra("ids");
			setTitle("Filtro: " + intent.getStringExtra("filter"));

			eventList = new ArrayList<Event>();
			EventDbHelper helper = new EventDbHelper(this);
			for (int i = 0; i < ids.size(); i++) {
				Cursor c = helper.getEventById(ids.get(i));
				c.moveToFirst();
				eventList.addAll(helper.cursorToEvents(c));
			}

			if (googlePlayServices) {
				helper.close();
				drawMarker(eventList);
				googleMap.setOnMarkerClickListener(this);

				mLocationClient = new LocationClient(this, this, this);
			}
			getSupportActionBar().setHomeButtonEnabled(true);
			getSupportActionBar().setDisplayHomeAsUpEnabled(true);

			mDrawer = (DrawerLayout) findViewById(R.id.activity_map_filter);
			initMenu();
			mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
			mDrawer.setDrawerListener(mDrawerToggle);

		}

	}

	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	@Override
	protected void moveCamera(LatLng latLng) {
		if (refresh) {
			CameraPosition pos = new CameraPosition(latLng, 11, 0, 0);
			CameraUpdate cameraUpdate = CameraUpdateFactory
					.newCameraPosition(pos);
			googleMap.animateCamera(cameraUpdate);
		} else {
			// set default boundary latitude and longitude to some point
			double minLat = location.getLatitude();
			double maxLat = location.getLatitude();
			double minLon = location.getLongitude();
			double maxLon = location.getLongitude();

			for (Event e : eventList) {

				maxLat = Math.max(Double.parseDouble(e.getLat()), maxLat);
				minLat = Math.min(Double.parseDouble(e.getLat()), minLat);
				maxLon = Math.max(Double.parseDouble(e.getLng()), maxLon);
				minLon = Math.min(Double.parseDouble(e.getLng()), minLon);

			}

			LatLngBounds latLonBound = new LatLngBounds(new LatLng(minLat,
					minLon), new LatLng(maxLat, maxLon));

			try {
				// set zoom level to show all markers with padding 50
				googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
						latLonBound, 70));
			} catch (Exception e) {
				Display display = getWindowManager().getDefaultDisplay();
				Point size = new Point();
				int width, height;
				if (android.os.Build.VERSION.SDK_INT >= 13) {
					display.getSize(size);
					width = size.x;
					height = size.y;
				} else {
					width = display.getWidth();
					height = display.getHeight();
				}
				googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
						latLonBound, width, height, 70));
			}
		}
	}

	@Override
	protected void download() {
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void myClass() {
		Intent intent = new Intent(context, MapActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}

	@Override
	public void onConnected(Bundle bundle) {
		super.onConnected(bundle);
	}

	@Override
	public void onConnectionFailed(ConnectionResult connectionResult) {
		// TODO Auto-generated method stub
		super.onConnectionFailed(connectionResult);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	public boolean onMarkerClick(Marker marker) {
		// TODO Auto-generated method stub
		return super.onMarkerClick(marker);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public void onDisconnected() {
		// TODO Auto-generated method stub
		super.onDisconnected();
	}

	@Override
	public void navigationRefresh(View view) {
		// TODO Auto-generated method stub
		super.navigationRefresh(view);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mDrawer.closeDrawer(mDrawerList);
	}
}
